package com.ckzp.core.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.cookie.GlobalCookieManager;
import com.ckzp.core.json.Jmode;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by 51594 on 2022/7/27.
 */
public class CookieUtil {

    /**
     * \
     *
     * @param response
     * @param cookieName
     * @param value
     */
    public static void setCookie(HttpServletResponse response, String cookieName, String value) {
        try {
            Cookie cookie = new Cookie(cookieName, value);
            cookie.setMaxAge(1 * 24 * 60 * 60);
            cookie.setPath("/");
            response.addCookie(cookie);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * \
     *
     * @param response
     * @param cookieName
     * @param value
     */
    public static void setSecureCookie(HttpServletResponse response, String path, String cookieName, String value) {
        try {
            Jmode cookie = Jmode.n().set("name", cookieName).set("value", Base64.encode(value));
//        cookie.setSecure(true);
            cookie.set("maxage", 1 * 24 * 60 * 60);
            cookie.set("path", StrUtil.isBlank(path) ? "/" : path);
            addCookie(response, cookie, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置HttpOnly Cookie
     *
     * @param response   HTTP响应
     * @param cookie     Cookie对象
     * @param isHttpOnly
     */
    public static void addCookie(HttpServletResponse response, Jmode cookie, boolean isHttpOnly) {
        String name = cookie.getStr("name");//Cookie名称
        String value = cookie.getStr("value");//Cookie值
        int maxAge = cookie.getInt("maxage");//最大生存时间(毫秒,0代表删除,-1代表与浏览器会话一致)
        String path = cookie.getStr("path");//路径
        String domain = new Cookie(name, value).getDomain();//域
        boolean isSecure = false;//是否为安全协议信息

        StringBuilder buffer = new StringBuilder();

        buffer.append(name).append("=").append(value).append(";");

        if (maxAge == 0) {
            buffer.append("Expires=Thu Jan 01 08:00:00 CST 1970;");
        } else if (maxAge > 0) {
            buffer.append("Max-Age=").append(maxAge).append(";");
        }

        if (domain != null) {
            buffer.append("domain=").append(domain).append(";");
        }
        if (path != null) {
            buffer.append("path=").append(path).append(";");
        }
        if (isSecure) {
            buffer.append("secure;");
        }
        if (isHttpOnly) {
            buffer.append("HTTPOnly;");
        }
        response.addHeader("Set-Cookie", buffer.toString());
    }


    /**
     * \
     *
     * @param response
     * @param cookieName
     */
    public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String cookieName) {
        Cookie cookies[] = request.getCookies();
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                Cookie sCookie = cookies[i];
                String sname = sCookie.getName();
                if (sname.equals(cookieName)) {
                    sCookie.setMaxAge(0);
                    sCookie.setPath("/");
                    response.addCookie(sCookie);
                }
            }
        }
    }


    /**
     * \
     *
     * @param response
     */
    public static void emptyCookie(HttpServletRequest request, HttpServletResponse response) {
        Cookie cookies[] = request.getCookies();
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                Cookie sCookie = cookies[i];
                sCookie.setPath("/");
                response.addCookie(sCookie);
            }
        }
    }

    /**
     * @param request
     * @param cookieName
     * @return
     */
    public static String getSecureCookie(HttpServletRequest request, String cookieName) {
        Cookie cookies[] = request.getCookies();
        Cookie sCookie = null;
        String value = "";
        String svalue = null;
        String sname = null;
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                sCookie = cookies[i];
                svalue = sCookie.getValue();
                sname = sCookie.getName();
                if (sname.equals(cookieName)) {
                    value = Base64.decodeStr(svalue);
                }
            }
        }
        return value;
    }

    /**
     * @param request
     * @param cookieName
     * @return
     */
    public static String getCookie(HttpServletRequest request, String cookieName) {
        Cookie cookies[] = request.getCookies();
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                Cookie sCookie = cookies[i];
                if (sCookie.getName().equals(cookieName)) {
                    return sCookie.getValue();
                }
            }
        }
        return "";
    }
}
